#!/bin/bash


# === BTRBK ===

#btrfs subvolumes mount points
BTRFS_LIST=$(findmnt -nt btrfs | sed "s/^[^/]*//" | sed "s/ .*$//")

if [ -n "$BTRFS_LIST" ] # Subvolumes test
then

apt -y install btrbk

for BTRFS_PATH in $BTRFS_LIST   # Subvolumes loop
do

if [ -d $BTRFS_PATH ]   # Path test
then

echo $BTRFS_PATH

if [ "$BTRFS_PATH" = "/" ]
then
BTRFS_NAME=root
else
BTRFS_NAME=$(echo $BTRFS_PATH | sed "s/^.*\///")
fi

if [ -n "$BTRFS_NAME" ] # Name test
then

echo $BTRFS_NAME

sudo mkdir $BTRFS_PATH/btrbk_snap

if [ -d $BTRFS_PATH ]   # Snap dir test
then

sudo sh -c "echo '
timestamp_format        long

snapshot_dir               btrbk_snap

volume $BTRFS_PATH
  subvolume  .
' > /etc/btrbk/$BTRFS_NAME.conf"

sudo sh -c "echo '[Desktop Entry]
Type=Application
Name=BTRFS $BTRFS_NAME Backup
Exec=pkexec btrbk -c /etc/btrbk/$BTRFS_NAME.conf -q run
Icon=btrbk
Terminal=false
' > /usr/share/applications/BtrBackup.$BTRFS_NAME.desktop"

fi  # Snap dir test

fi  # Name test

fi  # Path test

done    # Subvolumes loop

# Daily home backup

if [ -r /etc/btrbk/home.conf ]  # Home test
then

sudo sh -c "echo '
#transaction_log            /var/log/btrbk.log

snapshot_preserve_min   7d
snapshot_preserve       14d

' > /etc/btrbk/btrbk.conf"

sudo cat /etc/btrbk/home.conf >> /etc/btrbk/btrbk.conf

if [ -r /etc/btrbk/btrbk.conf ]   # Conf file test
then

sudo sh -c "echo '#!/bin/sh

USAGE_LIMIT=97

BTRFS_LIST=\$(findmnt -nt btrfs | sed \"s/^[^/]*//\" | sed \"s/ .*\$//\")

for BTRFS_PATH in \$BTRFS_LIST
do
	if [ -d \$BTRFS_PATH/btrbk_snap ]
	then
		echo \$BTRFS_PATH
		rm \$BTRFS_PATH/btrbk_snap/size-reserve 2> /dev/null
		
		# Remove snapshots but first one a day
		
		unset last
		for item in \$(ls -1 -d \$BTRFS_PATH/btrbk_snap/*.*T*/ 2> /dev/null)
		do
			date=\$(echo \$item | grep -Eo \"[0-9]{8}\")
			
			if [ \"\$date\" = \"\$last\" ]
			then
				echo Remove \$item
				btrfs subvolume delete \$item
			else
				echo Keep \$item
				last=\$date
			fi
		done
		
		# Remove old snapshots if disk is full
		
		unset first
		unset last
		for SNAP in \$(ls -1 -r -d \$BTRFS_PATH/btrbk_snap/*.*T*/ 2> /dev/null)
		do
			if [ -z \"\$last\" ]
			then
				last=\$SNAP		# Keep it anyway
			else
				first=\$SNAP		# The oldest snap to remove
			fi
		done
				
		if [ -n \"\$first\" ]
		then
			USAGE=\$(df \$BTRFS_PATH | grep / | awk \"{ print \\\$5}\" | sed 's/%//g')
			echo USAGE \$USAGE
			if [ \"\$USAGE\" -gt \"\$USAGE_LIMIT\" ]
			then
				echo Remove \$first
				btrfs subvolume delete \$first
			fi
		fi
		
		dd if=/dev/urandom of=\$BTRFS_PATH/btrbk_snap/size-reserve bs=1M count=20 2> /dev/null
	fi
done

' > /usr/local/bin/btrbk-clean.sh"

sudo chmod +x /usr/local/bin/btrbk-clean.sh


sudo sh -c "echo '#!/bin/sh

date -Iseconds > /var/log/btrbk-hourly.log

sh /usr/local/bin/btrbk-clean.sh >> /var/log/btrbk-hourly.log

' > /etc/cron.hourly/btrbk-clean"

sudo chmod +x /etc/cron.hourly/btrbk-clean


sudo sh -c "echo '#!/bin/sh

exec btrbk -q run

' > /etc/cron.daily/btrbk-run"

sudo chmod +x /etc/cron.daily/btrbk-run

fi  # Conf file test

fi  # Home test

fi  # Subvolumes test

# Home backup

if [ -r /etc/btrbk/btrbk.conf ]  # Home test
then

sudo btrbk -q run

fi

# Root backup

if [ -r /etc/btrbk/root.conf ]  # Root test
then

sudo btrbk -c /etc/btrbk/root.conf -q run

fi



